﻿using System;
using System.Windows;

using ManageSystemClient.HttpService;
using ManageSystemClient.WindowService;

using Microsoft.Extensions.DependencyInjection;

namespace ManageSystemClient
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        private readonly UserService userService;
        private readonly IServiceProvider serviceProvider;
        public LoginWindow(IServiceProvider provider)
        {
            serviceProvider = provider;
            userService = provider.GetRequiredService<UserService>();
            InitializeComponent();
        }

        private void DisableComponents()
        {
            idText.IsEnabled = false;
            passwordText.IsEnabled = false;
            loginButton.IsEnabled = false;
            registerButton.IsEnabled = false;
            progressBar.Visibility = Visibility.Visible;
            progressBar.IsEnabled = true;
        }

        private void EnableComponents()
        {
            idText.IsEnabled = true;
            passwordText.IsEnabled = true;
            loginButton.IsEnabled = true;
            registerButton.IsEnabled = true;
            progressBar.Visibility = Visibility.Hidden;
            progressBar.IsEnabled = false;
        }

        private void ClickLogin(object sender, RoutedEventArgs e)
        {
            if (!uint.TryParse(idText.Text, out uint id))
            {
                _ = MBox.ShowError(this, "Invalid ID.");
                return;
            }
            DisableComponents();
            _ = userService.LoginAsync(id, passwordText.Password)
                .ContinueWith(task =>
                {
                    Dispatcher.Invoke(() =>
                    {
                        if (task.IsFaulted)
                        {
                            _ = MBox.ShowError(this, task.Exception!.GetBaseException().Message);
                            EnableComponents();
                        }
                        else
                        {
                            serviceProvider.GetRequiredService<NaviWindow>().Show();
                            Close();
                        }
                    });
                });
        }

        private void RegisterClick(object sender, RoutedEventArgs e)
        {
            if (!uint.TryParse(idText.Text, out uint id))
            {
                _ = MessageBox.Show(this, "Invalid ID.");
                return;
            }
            DisableComponents();
            _ = userService.RegisterAsync(id, passwordText.Password)
                .ContinueWith(task =>
                {
                    Dispatcher.Invoke(() =>
                    {
                        if (task.IsFaulted)
                        {
                            _ = MBox.ShowError(this, task.Exception!.GetBaseException().Message);
                        }
                        else
                        {
                            _ = MBox.ShowInfo(this, "Successfully registered.");
                        }
                        EnableComponents();
                    });
                });
        }
    }
}
